<template>
  <div class="store">
    <header class="header">
      <van-icon name="search"  @click.stop="$toast(api.msg)"/>
      <div>商城</div>
      <van-icon @click.stop="$router.push('/myshop')" class="icon" name="shopping-cart-o" />
    </header>
    <div class="tags" v-if="tags">
      <div
        class="tag"
        v-for="tag in tags"
        :key="tag.id"
        @click="toTagDetail(tag)"
      >
        <div class="img-box">
          <img :src="tag.i" alt="" />
        </div>
        <div class="title">{{ tag.n }}</div>
      </div>
    </div>
    <div class="content">
      <div class="item">
        <TitleItem :title="'全部课程'" :rightText="''" />
        <div class="item-list">
          <StoreItem
            v-for="(storeItem, index) in allStoreList"
            :key="storeItem.rp.store_id + '' + index"
            :storeItem="storeItem.rp"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import TitleItem from "@/components/TitleItem.vue";
import StoreItem from "@/components/StoreItem.vue";
export default {
  components: {
    TitleItem,
    StoreItem,
  },
  data: () => {
    return {
      offset: 0,
      tags: null,
      allStoreList: [],
      myShop:[]
    };
  },
  methods: {
    toTagDetail(tag) {
      localStorage.setItem("storetag", tag.n);
      this.$router.push({
        name: "storetag",
        params: {
          tagid: tag.id,
        },
      });
    },
    getStoreTags() {
      this.axios({
        method: "post",
        url: `${this.api.baseUrlV2}/extend/indexhv1/v1`,
        headers: {
          version: this.api.version,
        },
      }).then((res) => {
        this.tags = res.data.result.cs;
      });
    },
    getAllStoreList() {
      this.axios({
        method: "post",
        url: `${this.api.baseUrlV2}/products/allhv1/${this.offset}/30/v1`,
        headers: {
          version: this.api.version,
        },
      }).then((res) => {
        this.allStoreList = [...this.allStoreList, ...res.data.result.rps];
      });
    },
  },
  activated(){
    this.myShop = JSON.parse(localStorage.getItem("myShop"))||[];
  },
  created() {
    this.getStoreTags();
    this.getAllStoreList();
  },
};
</script>

<style lang="scss" scoped>
.store {
  width: 100%;
  padding-bottom: 0.5rem;
  .header {
    position: sticky;
    left: 0;
    top: 0;
    z-index: 100;
    width: 100%;
    height: 0.55rem;
    background-color: var(--bgc-white);
    box-sizing: border-box;
    border-bottom: 0.01rem solid var(--border-gray);
    text-align: center;
    line-height: 0.55rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 0.15rem;
    font-size: 0.24rem;
    > div {
      font-size: 0.18rem;
    }
  }
  .tags {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .tag {
      width: 20%;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      margin: 0.05rem 0;
      .img-box {
        width: 0.3rem;
        height: 0.3rem;
      }
      .title {
        font-size: 0.12rem;
        color: var(--text-black);
      }
    }
  }
  .content {
    width: 100%;
    box-sizing: border-box;
    padding: 0 0.2rem;
    .item-list {
      width: 100%;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
    }
  }
}
</style>